<?php

namespace app\admin\controller;

use think\Db;
use think\Request;

class Active extends Base
{
    /**
     * 活动列表展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $params = input('param.');
            $where = [];
            
            // 活动名称
            if(isset($params['active_name']) && $params['active_name'] != ''){
                $where['active_name'] = ['like', '%' . $params['active_name'] . '%'];
            }
            
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('active')->where($where)->count();// 获取总条数
            $lists = Db::name('active')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('active_start_time desc')
                ->select();
            
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 发布活动
     * @return mixed|\think\response\Json
     */
    public function activeAdd(){
        if(Request::instance()->isPost()){
            $active_name = input('active_name');
            $active_content = input('active_content');
            $active_image = input('active_image');
            $active_member_num = input('active_member_num');
            $active_address = input('active_address');
            $active_start_time = input('active_start_time');
            $active_end_time = input('active_end_time');
            if(strtotime($active_end_time) <= strtotime($active_start_time)){
                return json(['code' => 100, 'msg' => '结束时间不可以早于开始时间']);
            }
            $insert = [
                'active_image' => $active_image,
                'active_name' => $active_name,
                'active_content' => $active_content,
                'active_member_num' => $active_member_num,
                'active_address' => $active_address,
                'active_start_time' => $active_start_time,
                'active_end_time' => $active_end_time,
                'active_time' => date('Y-m-d H:i:s'),
                'active_update_time' => date('Y-m-d H:i:s')
            ];
            
            $res = Db::table('up_active')->insertGetId($insert);
            if($res){
                $ercode = $this->scerweima($res);
                Db::table('up_active')->where('active_id',$res)->setField('active_ercode',$ercode);
                return json(['code' => 200, 'msg' => '发布成功']);
            }else{
                return json(['code' => 100, 'msg' => '发布失败']);
            }
        }
        return $this->fetch('active/activeadd');
    }
    function scerweima($aid){
        $dir = $_SERVER['DOCUMENT_ROOT'];
        $url =  "http://".$_SERVER['HTTP_HOST'];
        require_once VENDOR_PATH . "phpqrcode.php";
        $rand = date('YmdHis').rand(100000,999999);
        $value = "$url/index/Index/hexiaoself?aid=$aid"; //二维码内容：链接
        $errorCorrectionLevel = 'L';    //容错级别
        $matrixPointSize = 8;           //生成图片大小
        //生成二维码图片
        $ymd = date('Ymd');
        $zip_path = "{$dir}/uploads/face/$ymd";
        if (!file_exists($zip_path)) {
            mkdir($zip_path);
        }
        $filename = "$zip_path/{$rand}.png";
        $filehttp = "{$url}/uploads/face/$ymd/{$rand}.png";
        \QRcode::png($value,$filename , $errorCorrectionLevel, $matrixPointSize, 2);
        $QR = $filename;              //已经生成的原始二维码图片文件
        $QR = imagecreatefromstring(file_get_contents($QR));
        //输出图片
//        imagepng($QR, 'qrcode.png');
        imagedestroy($QR);
        return $filehttp;
    }
    /**
     * 活动详情
     * @return mixed
     */
    public function activeDetail(){
        $active_id = input('active_id');
        $info = Db::table('up_active')
            ->where('active_id', $active_id)
//            ->field('active_image, active_name, active_content, active_member_num, active_address, active_start_time, active_end_time')
            ->find();
        $this->assign([
            'info' => $info
        ]);
        return $this->fetch('active/activedetail');
    }
    
    /**
     * 编辑活动
     * @return mixed|\think\response\Json
     */
    public function activeEdit(){
        $active_id = input('active_id');
        $today = date('Y-m-d H:i:s');
        if(Request::instance()->isPost()){
            $active_name = input('active_name');
            $active_content = input('active_content');
            $active_image = input('active_image');
            $active_member_num = input('active_member_num');
            $active_address = input('active_address');
            $active_start_time = input('active_start_time');
            $active_end_time = input('active_end_time');
            if(strtotime($active_end_time) <= strtotime($active_start_time)){
                return json(['code' => 100, 'msg' => '结束时间不可以早于开始时间']);
            }
            $update = [
                'active_image' => $active_image,
                'active_name' => $active_name,
                'active_content' => $active_content,
                'active_member_num' => $active_member_num,
                'active_address' => $active_address,
                'active_start_time' => $active_start_time,
                'active_end_time' => $active_end_time,
                'active_update_time' => date('Y-m-d H:i:s')
            ];
            if($active_end_time >= $today){
                $update['active_status'] = 1;
            }
            $res = Db::table('up_active')->where('active_id', $active_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        
        $info = Db::table('up_active')
            ->where('active_id', $active_id)
            ->field('active_image, active_name, active_content, active_member_num, active_address, 
            active_start_time, active_end_time')
            ->find();
        $this->assign([
            'active_id' => $active_id,
            'info' => $info
        ]);
        return $this->fetch('active/activeedit');
    }
    
    /**
     * 删除活动
     * @return \think\response\Json
     */
    public function activeDel(){
        $active_id = input('id');
        Db::startTrans();
        try{
            // 活动表
            Db::table('up_active')->where('active_id', $active_id)->delete();
            // 活动报名表
            Db::table('up_active_member')->where('am_active_id', $active_id)->delete();
            
            Db::commit();
            return json(['code' => 200, 'msg' => '删除成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    /**
     * 编辑活动
     * @return mixed|\think\response\Json
     */
    public function priceSet(){
        $active_id = input('active_id');
        if(Request::instance()->isPost()){
            $active_one = input('active_one');
            $active_one_img = input('active_one_img');
            $active_two = input('active_two');
            $active_two_img = input('active_two_img');
            $active_three = input('active_three');
            $active_three_img = input('active_three_img');
            $update = [
                'active_one' => $active_one,
                'active_one_img' => $active_one_img,
                'active_two' => $active_two,
                'active_two_img' => $active_two_img,
                'active_three' => $active_three,
                'active_three_img' => $active_three_img,
                'active_update_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_active')->where('active_id', $active_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $info = Db::table('up_active')
            ->where('active_id', $active_id)
            ->find();
        $this->assign([
            'active_id' => $active_id,
            'info' => $info
        ]);
        return $this->fetch('active/priceset');
    }
    /**
     * 编辑活动
     * @return mixed|\think\response\Json
     */
    public function price(){
        $active_id = input('active_id');
        $info = Db::table('up_active')
            ->where('active_id', $active_id)
            ->find();
        $this->assign([
            'active_id' => $active_id,
            'info' => $info
        ]);
        return $this->fetch('active/price');
    }
}